package com.zhuozhou.jdyc.util.update;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.zhuozhou.jdyc.util.apiUtil.FileUtils;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;
import com.zhuozhou.jdyc.util.update.callback.DownloadCallback;
import com.zhuozhou.jdyc.util.update.server.DownloadApkReceiver;
import com.zhuozhou.jdyc.util.update.server.DownloadFileReceiver;

import java.io.File;

import io.reactivex.disposables.Disposable;


/**
 * Created by 方舟 on 2020/08/07.
 * describe：文件下载
 */
public class DownloadManger {
    // 进度条与通知UI刷新的handler和msg常量
    private static DownloadManger updateManger;
    private DownloadFileReceiver downloadApkReceiver;

    private DownloadManger() {

    }

    public static DownloadManger getInstance() {
        if (updateManger == null) {
            synchronized (DownloadManger.class) {
                updateManger = new DownloadManger();
            }
        }
        return updateManger;
    }

    public void download(Activity mContext, String fileUrl) {
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //申请WRITE_EXTERNAL_STORAGE权限
            ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    0x01);
        } else {
            if (StringUtil.isEmpty(fileUrl)) {
                ToastUtils.showShort(mContext, "文件地址错误！");
                return;
            }
            if (downloadApkReceiver == null) {
                downloadApkReceiver = new DownloadFileReceiver();
            }
            mContext.registerReceiver(downloadApkReceiver, getIntentFilter());
            final Intent intent = new Intent();
            intent.putExtra(DownloadFileReceiver.NOTIFY_ID, fileUrl.hashCode());
            intent.putExtra(DownloadApkReceiver.IS_DOWNLOAD_APK, false);
            RxNet.download(fileUrl, RxNet.PATH + FileUtils.getFileName(fileUrl), new DownloadCallback() {
                @Override
                public void onStart(Disposable d) {
                    if (mContext != null && !mContext.isFinishing()) {
                        intent.setAction(RxNet.ACTION_START);
                        mContext.sendBroadcast(intent);
                    }
                }

                @Override
                public void onProgress(long totalByte, long currentByte, int progress) {
                    intent.setAction(RxNet.ACTION_UPDATE);
                    intent.putExtra(DownloadFileReceiver.PROGRESS_RATE, progress);
                    if (mContext != null && !mContext.isFinishing()) {
                        mContext.sendBroadcast(intent);
                    }
                }

                @Override
                public void onFinish(File file) {
                    intent.setAction(RxNet.ACTION_FINISHED);
                    intent.putExtra(DownloadFileReceiver.APK_FILE_PATH, file.getAbsolutePath());
                    if (mContext != null && !mContext.isFinishing()) {
                        mContext.sendBroadcast(intent);
                    }
                }

                @Override
                public void onError(String msg) {
                    intent.setAction(RxNet.ACTION_ERROR);
                    if (mContext != null && !mContext.isFinishing()) {
                        mContext.sendBroadcast(intent);
                    }
                }
            });
        }
    }

    private IntentFilter getIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(RxNet.ACTION_START);
        intentFilter.addAction(RxNet.ACTION_UPDATE);
        intentFilter.addAction(RxNet.ACTION_FINISHED);
        intentFilter.addAction(RxNet.ACTION_CANCEL);
        intentFilter.addAction(RxNet.ACTION_ERROR);
        intentFilter.addAction(RxNet.ACTION_REDIRECT_ERROR);
        return intentFilter;
    }

}
